﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Kaskelot.SP.UI.Analys.ViewModel
{
	public class Query : INotifyPropertyChanged
	{
		public Query()
		{
			IsPersonal = true;
			Filters = new List<IFilterPart>();
		}

		public int ID { get; set; }

		public string Name { get; set; }

		public List<Field> DisplayFields
		{
			get
			{
				List<Field> fields = new List<Field>();
				this.Entities.ForEach(e =>
					fields.AddRange(e.Fields.Where(f => f.Checked)));
				return fields;
			}
		}

		public List<IFilterPart> Filters { get; set; }

		private List<Entity> entities;
		public List<Entity> Entities
		{
			get
			{
				return this.entities;
			}
			set
			{
				this.entities = value;
				this.entities.ForEach(e =>
					e.PropertyChanged += entity_PropertyChanged);
			}
		}

		public List<Entity> PrimaryEntities
		{
			get
			{
				return this.Entities.Where(e => 
					e.Name != "Kontaktperson" &&
 					e.Name != "Materiel" &&
					e.Name != "Uppdrag" &&
					e.Name != "Lärare" &&
					e.Name != "Lokal").ToList();
			}
		}

		void entity_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName != "IsEnabled")
			{
				OnPropertyChanged("Entities");
				OnPropertyChanged("PrimaryEntity");
			}
		}

		public bool IsPersonal { get; set; }

		public event PropertyChangedEventHandler PropertyChanged;
		private void OnPropertyChanged(string propertyName)
		{
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
			}
		}
	}
}
